<template>
  <div class="padding-top-20">
    <template slot="title">
      <div class="font-size-16 font-weight-700 padding-left-10">基本信息</div>
    </template>
    <form-common v-if="buildFiledList.length > 0" @remoteMethod="remoteMethod" :filed-list.sync="buildFiledList" @selectChange="selectChange" ref="form" @rightBtnFn="rightBtnFn"></form-common>

    <!-- projectStatus 1:草稿 2:已开通 -->
    <div class="margin-top-20" v-if="(!model.projectStatus || model.projectStatus == 1) && !$route.query.processWorkId">
      <el-button size="mini" class="f-right" type="primary" @click="openSaveInfo">保存</el-button>
    </div>

    <AddressBMap :mapVisible="mapDialog" v-on:mapLocationClose="mapDialog = false" v-on:mapLocationSave="mapLocationSave"></AddressBMap>
  </div>
</template>
<script>
  import projectInfoApi from '../../../api/projectInfo';
  import formCommon from '@g/components/form/formCommon.vue';
  import { dictList, provinceList, cityList, areaList, assetPackage } from '../../../api/common';
  import { buildFiledList } from '../form.js';
  import AddressBMap from '@/common/components/AddressBMap';
  import { requestTips, askDialog } from '../../../assets/util';

  export default {
    props: {
      spaceKey: {
        type: String,
        default: '',
      },
    },
    data() {
      return {
        mapDialog: false,
        buildFiledList: [],
        currentFiledItem: {}, //表单组件中返回的信息
        model: {},
      };
    },
    computed: {
      isPageEdit() {
        return !this.$route.query.taskId;
      },
    },
    mounted() {
      // this.init();
    },

    methods: {
      init() {
        this.getDictionary();
        // this.getProvinceList();
        this.getProjectList();
        if (this.spaceKey || this.$route.query.spaceKey) {
          this.getInfo();
        } else {
          this.buildFiledList = buildFiledList
        }
      },

      //字典
      async getDictionary() {
        let res = await dictList({
          dictType: 'land_type',
        });
        buildFiledList.forEach(item => {
          if (item.bindFieldName == 'landType') {
            item.optionList = res.rows || [];
          }
          if (!this.isPageEdit) {
            item.isDisabled = true;
          }
        });
      },
      // 获取省
      async getProvinceList() {
        let res = await provinceList({});
        buildFiledList.forEach(item => {
          if (item.bindFieldName == 'provinceCode') {
            item.optionList = res.rows || [];
          }
        });
      },
      // 获取市
      async getCityList(provinceCode) {
        let res = await cityList({ provinceCode: provinceCode });
        buildFiledList.forEach(item => {
          if (item.bindFieldName == 'cityCode') {
            item.optionList = res.rows || [];
          }
        });
      },
      // 获取区
      async getAreaList(cityCode) {
        let res = await areaList({ cityCode: cityCode });
        buildFiledList.forEach(item => {
          if (item.bindFieldName == 'areaCode') {
            item.optionList = res.rows || [];
          }
        });
      },
      //获取项目
      async getProjectList(initbuildFiledList) {
        let res = await projectInfoApi.apiAllProjectList({
          status: 1,
        });
        if (initbuildFiledList && initbuildFiledList.length) {
          initbuildFiledList.forEach(item => {
            if (item.bindFieldName == 'projectKey') {
              item.optionList = res.rows || [];
            }
          });
        }

        this.projectList = res.rows || [];
      },
      // 切换
      selectChange(data) {
        if (data.filedName == 'provinceCode') {
          this.$refs['form'].setFiledSource('cityCode', '');
          this.$refs['form'].setFiledSource('areaCode', '');
          this.buildFiledList.forEach(item => {
            if (item.bindFieldName == 'areaCode') {
              item.optionList = [];
            }
          });
          this.getCityList(data.filedVal);
        } else if (data.filedName == 'cityCode') {
          this.$refs['form'].setFiledSource('areaCode', '');
          this.getAreaList(data.filedVal);
        }
      },
      remoteMethod(val) {
        projectInfoApi.managerlist({ employeeName: val.remoteVal }).then(res => {
          if (res.success) {
            val.cbList(val.formItem.bindFieldName, res.rows);
          } else {
            val.cbList(val.formItem.bindFieldName, []);
          }
        });
      },
      //详情
      async getInfo() {
        let initbuildFiledList = buildFiledList;
        this.buildFiledList = [];
        let res = await projectInfoApi.apiProjectInfo({
          spaceKey: this.spaceKey || this.$route.query.spaceKey,
        });

        await this.getProjectList(initbuildFiledList);

        this.model = res;

        this.setFiledVal(initbuildFiledList, res);
        // this.getCityList(res.provinceCode);
        // this.getAreaList(res.cityCode);
        this.$nextTick(() => {
          this.buildFiledList = initbuildFiledList;
        });
      },

      //小区地址选择回调
      mapLocationSave(e) {
        this.currentFiledItem.cb(this.currentFiledItem.filedName, e.address);
        this.currentFiledItem.model.lng = e.longitude;
        this.currentFiledItem.model.lat = e.latitude;
        this.mapDialog = false;
      },
      //修改信息
      async openSaveInfo() {
        let model = this.$refs.form.validateForm();
        console.log(model)
        if (!model) return;
        let paramData = Object.assign({}, model);

        paramData.startYear = new Date(paramData.startYear).getFullYear();
        paramData.primaryKey = this.model.primaryKey;
        paramData.spaceKey = this.model.spaceKey;
        paramData.manager = paramData.managerName;
        let res = await projectInfoApi.apiProjectUpdate(paramData);
        requestTips(res, () => {
          this.getInfo();
        });
      },
      //点击地图回掉
      rightBtnFn(data) {
        this.currentFiledItem = data;
        this.mapDialog = true;
      },
      //回显
      setFiledVal(list, row) {
        list.forEach(item => {
          for (let i in row) {
            if (i == item.bindFieldName) {
              item.bindFieldVal = row[i];
            }
          }
        });
      },
    },

    components: { AddressBMap, formCommon },
  };
</script>
<style lang="scss" scoped>
  .left-lable {
    width: 120px;
  }
</style>
